<form id="userRoleForm" class="form-horizontal" action="${ctx}/user/saveUserRole.do" method="post">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h5 class="blue">设置用户权限</h5>
    </div>
    <input type="hidden" name="userId" id="userId" class="col-xs-12 col-sm-4 required" value="${userId!}"/>

    <div class="modal-body">
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="username">用户名:</label>

            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="username" id="username" class="col-xs-12 col-sm-4 required"
                           value="${user.username!}"/>
                </div>
            </div>
        </div>

        <div class="form-group">
            <div id="rescTree" class="ztree"></div>
        </div>

        <div class="modal-footer center">
            <button type="button" class="btn btn-sm" data-dismiss="modal"><i class="ace-icon fa fa-times"></i> 关闭
            </button>
        </div>
    </div>
</form>
<script src="${ctx}/static/admin/assets/js/jquery.validate.js"></script>
<script src="${ctx}/static/admin/assets/js/jquery.validate.messages_zh.js"></script>
<script src="${ctx}/static/admin/assets/js/jquery.ztree.all-3.5.min.js"></script>
<link rel="stylesheet" href="${ctx}/static/admin/assets/css/zTreeStyle/zTreeStyle.css" type="text/css">

<script type="text/javascript">
    $(document).ready(function () {

        $.get("${ctx}/user/getResources.do?userId=${user.id!}", function (data) {
            $.fn.zTree.init($("#rescTree"), setting, data.data);
        });

        //权限树 相关事件
        function onCheckHandler(event, treeId, treeNode) {
            if (treeNode.checked) {
                // 增加权限
                $.post("${ctx}/user/addUserResource.do", {"userId": treeNode.userId, "resourceIds": treeNode.id});
            }
            else {
                // 移除权限
                $.post("${ctx}/user/deleteUserResource.do", {"userId": treeNode.userId, "resourceIds": treeNode.id});
            }
        }

        // 权限树
        var setting = {
            view: {
                dblClickExpand: true,
                showIcon: false
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: 0
                }
            },
            check: {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: {"Y": "", "N": ""},
                autoCheckTrigger: true
            },
            callback: {
                onCheck: onCheckHandler
            }
        }


        $(".date-picker").datepicker({
            language: 'zh-CN',
            autoclose: true,
            todayHighlight: true
        });


        $("#userRoleForm").validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: true,
            highlight: function (e) {
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function (e) {
                $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                $(e).remove();
            },
            errorPlacement: function (error, element) {
                if(element.is('input[type=checkbox]') || element.is('input[type=radio]')) {
                    var controls = element.closest('div[class*="col-"]');
                    if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
                    else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                }
                else if(element.is('.select2')) {
                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                }
                else if(element.is('.chosen-select')) {
                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                }
                else error.insertAfter(element.parent());
            },

            submitHandler: function (form) {
                $.ajax({
                    cache: true,
                    type: "POST",
                    url:"${ctx}/user/saveUserRole.do",
                    data:$('#userRoleForm').serialize(),// 你的formid
                    async: true,
                    error: function(request) {
                        alert("保存失败－请求出错");
                    },
                    success: function(data) {
                        if(data.result == "success"){
                            window.location="${refer!}";
                        }else{
                            alert(msg.messages);
                        }
                    }
                });
            },
            invalidHandler: function (form) {
            }
        })
    });
</script>